<template>
  <div :id="chartId" class="chart-box" />
</template>

<script>
import echarts from "echarts";
import resize from "./mixins/resize";
export default {
  mixins: [resize],
  props: {
    chartTitle: {
      type: String,
      default: "title",
    },
    chartId: {
      type: String,
      default: "chart",
    },
    pieList: {
      type: Array,
      default: function () {
        return [];
      },
    },
    pieName: {
      type: String,
      default: "统计",
    },
  },
  data() {
    return {
      chart: null,
      name: "统计",
    };
  },
  watch: {
    pieList: {
      handler(val) {
        this.$nextTick(() => {
          this.initChart(val);
        });
      },
      immediate: true,
      deep: true,
    },
  },

  methods: {
    initChart(data) {
      let list = [];
      if (data && data.length > 0) {
        list = data.map((el) => {
          const label = el.label ? el.label : " ";
          const rate = el.scoreRate ? el.scoreRate : el.countRate;
          return {
            name: label,
            value: rate,
          };
        });
      }

      this.chart = echarts.init(document.getElementById(this.chartId));
      const option = {
        //color: ['#f5898b','#3BA1FF', '#FBD438', '#62DAAB', '#f5d69f'],
        color: ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272'],
        title: {
          text: this.chartTitle,
          subtext: '',
          left: 'center'
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {d}%",
        },
        legend: {
          //bottom: "5%",
          show: false,
          orient: "vertical",
          left: "left",
          textStyle: {
            // 图例文字的样式
            color: "#222",
            fontSize: 12,
          },
        },
        series: [
          {
            name: this.pieName,
            type: "pie",
            radius: ["30%", "55%"],
            center: ["50%", "50%"],
            avoidLabelOverlap: true,
            hoverAnimation: false,
            label: {
              show: true,
              formatter: "{b|{b}} \n {d|{d}%}",
              rich: {
                b: {
                  color: "#333333",
                  fontSize: 14,
                  lineHeight: 20,
                  align: 'left',
                },
                d: {
                  color: '#333333',
                  fontSize: 16,
                  lineHeight: 24,
                  height: 24
                },
              },
              textStyle: {
                color: "#333333",
                align: "right",
                fontSize: 14,
              },
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            data: list,
            emphasis: {
              itemStyle: {
                shadowBlur: 5,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
};
</script>

<style lang="scss" scoped>
.chart-box {
  width: 100%;
  min-height: 200px;
}
</style>
